<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Auth登陆注册 控制器
 */
class Auth extends Front_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->service('user/user_auth_service', 'auth');

        //开发模式开启调试信息
        if (defined('ENVIRONMENT') && ENVIRONMENT == 'development') $this->output->enable_profiler(true);
        log_message('debug', "Auth Controller Class Initialized");
    }

    public function index() {
        $data = array();
        $data['message'] = $this->session->flashdata('message');

        echo $data['message'];
    }

    /**
     * 注册页面
     */
    public function register() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', '用户账号', 'required');
        //$this->form_validation->set_rules('password', '用户密码', 'required');
        //$this->form_validation->set_rules('repassword', '确认密码', 'required|matches[password]');
        $this->form_validation->set_rules('email', '用户邮箱', 'valid_email');

        //表单验证成功, 获取表单数据
        if ($form_validation = $this->form_validation->run()) {
            $username = $this->input->post('username', true);
            $password = $this->input->post('password', true);
            $email = $this->input->post('email', true);
        }
        //表单验证成功, 开始注册
        if ($form_validation && $this->auth->register($username, $password, $email)) {
            $this->session->set_flashdata('message', $this->auth->messages());
            redirect('demo/auth/index', 'refresh');

            //$data = array();
            //$data['message'] = $this->auth->messages();
            //$this->load->view('demo/auth_register_success', $data);
        } else {
            $data = array();

            //错误消息
            if (validation_errors()) {
                $data['message'] = validation_errors();
            } else {
                //$data['message'] = $this->auth->errors() ? $this->auth->errors() : $this->session->flashdata('message');
                $data['message'] = $this->session->flashdata('message');
            }

            $this->load->view('demo/auth_register', $data);
        }
    }

    /**
     * 发送激活邮件
     */
    public function send_active_email() {

    }


    /**
     * 登录页面
     */
    public function login() {

    }

    /**
     * 验证用户名
     *
     */
    public function check_username() {
        $username = $this->input->post('username', true);

        if (($error = $this->auth->check_username($username)) instanceof Error) {
            $this->show_error($error->get_error());
        }
        $this->show_success();
    }

    /**
     * 验证密码复杂度是否符合
     */
    public function check_password() {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        if (($error = $this->auth->check_password($password, $username)) instanceof Error) {
            $this->show_error($error->get_error());
        }
        $this->show_success();
    }

    /**
     * 验证密码强度
     */
    public function check_password_strong() {
        $password = $this->input->post('password', true);
        $rank = $this->auth->check_password_strong($password);
        $this->show_success($rank);
    }

    /**
     * 验证Email
     */
    public function check_email() {
        $email = $this->input->post('email', true);
        if (($error = $this->auth->check_email($email)) instanceof Error) {
            $this->show_error($error->get_error());
        }
        $this->show_success();
    }

    /**
     * 验证手机号码
     */
    public function check_mobile() {

    }

    /**
     * 验证验证码
     */
    public function check_captcha() {

    }
}

/* End of file auth.php */
/* Location: ./application/controllers/demo/auth.php */
